import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import { commonRoutes } from './commonRoutes'

// 全局路由(无需嵌套上左右整体布局)
export const globalRoutes = [
  {
    path: '/404',
    component: () => import('@/views/common/404.vue'),
    name: '404',
    meta: { title: '404未找到' }
  },
  {
    path: '/login',
    component: () => import('@/views/common/login.vue'),
    name: 'login',
    meta: { title: '登录' }
  }
]

// 静态固定路由
export const staticRoute = commonRoutes

// 主入口路由(需嵌套上左右整体布局)
export const mainRoutes = {
  path: '/',
  component: () => import('@/views/main/index.vue'),
  name: 'main',
  meta: { title: '主入口整体布局' },
  children: []
}

const createRouter = () =>
  new VueRouter({
    mode: 'hash',
    scrollBehavior: () => ({
      y: 0
    }),
    routes: globalRoutes.concat(mainRoutes)
  })

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

const router = createRouter()

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export default router
